package com.example.demo.thread.pool;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @Description: 使用线程池 但不建议直接使用Executors去创建 而是自己去构造ThreadPoolExecutor,这里面的几个参数救很重要
 * @Author: LLL
 * @Date: 2020-03-20 15:16
 **/
public class MyThreadDemo {
    public static void main(String[] args) {
        // 12
        System.out.println(Runtime.getRuntime().availableProcessors());
        //        ExecutorService executorService = Executors.newSingleThreadExecutor();
        //        ExecutorService executorService = Executors.newFixedThreadPool(10);
        ExecutorService executorService = Executors.newCachedThreadPool();
        try {
            for (int i = 1; i <= 10; i++) {
                int finalI = i;
                executorService.execute(() -> {
                    System.out.println(Thread.currentThread().getName() + "处理" + finalI);
                });
            }

        } catch (Exception e) {

        } finally {
            executorService.shutdown();
        }
    }
}
